﻿using Avalonia.BiliBili.Libs.Models;
using Avalonia.BiliBili.Windows;
using Avalonia.Extensions.Controls;
using Bilibili.Common.Log;
using Bilibili.Model.Common;
using Bilibili.Model.Dynamic;
using Bilibili.WebClient.Api;
using PCLUntils.Objects;
using ReactiveUI;
using System;
using System.Threading.Tasks;

namespace Avalonia.BiliBili.ViewModels
{
    public sealed class DynamicInfoViewModel : ViewModelBase
    {
        private DynamicClient Client { get; }
        private DynamicCards dynamicCards;
        private DynamicItemCard dynamicItemCard;
        private readonly DynamicInfoWindow dynamicInfoWindow;
        public DynamicCards DynamicCards
        {
            get => dynamicCards;
            set => this.RaiseAndSetIfChanged(ref dynamicCards, value);
        }
        public DynamicItemCard DynamicItemCard
        {
            get => dynamicItemCard;
            set => this.RaiseAndSetIfChanged(ref dynamicItemCard, value);
        }
        public DynamicCardsData DynamicCardsData { get; set; }
        public DynamicInfoViewModel(DynamicInfoWindow dynamicInfoWindow)
        {
            Client = new DynamicClient();
            this.dynamicInfoWindow = dynamicInfoWindow;
        }
        public async Task<string> GetDynamicId(string doc_id)
        {
            try
            {
                var result = await Client.GetDynamicId(doc_id);
                if (result.IsNotEmpty())
                {
                    if (result.Success)
                        return result.Data;
                    else
                        PopupToast.Show("无法读取动态");
                }
            }
            catch (Exception ex)
            {
                PopupToast.Show("无法读取动态");
                LogManager.Instance.LogError("GetDynamicId", ex);
            }
            return string.Empty;
        }
        public async Task LoadDynamicData(string albumId)
        {
            var result = await Client.LoadDynamicData(albumId);
            if (result.Success)
            {
                var obj = result.Data;
                if (obj["code"].ToInt32() == 0)
                {
                    DynamicCards = obj["data"]["card"].ToObject<DynamicCards>();
                    LoadDynamicView();
                    LoadDynamicComment();
                }
            }
            else
                PopupToast.Show(result.Message);
        }
        public void LoadDynamicComment()
        {
            long id = 0, type = 0;
            switch (DynamicCards.Desc.Type)
            {
                case 1:
                    if (DynamicCardsData.ShowOrg)
                    {
                        switch (DynamicCards.Desc.Orig_Type)
                        {
                            case 2:
                                type = 11;
                                break;
                            case 4:
                            case 2048:
                                type = 17;
                                break;
                        }
                        id = DynamicCards.Desc.Orig_Dy_Id;
                    }
                    else
                    {
                        type = 17;
                        id = DynamicCards.Desc.Dynamic_Id;
                    }
                    break;
                case 4:
                case 2048:
                    type = 17;
                    id = DynamicCards.Desc.Dynamic_Id;
                    break;
                case 2:
                    type = 11;
                    id = DynamicCards.Desc.Rid;
                    break;
                case 16:
                    type = 5;
                    id = DynamicCards.Desc.Rid;
                    break;
            }
            dynamicInfoWindow.InitComment((CommentType)type, id.ToString());
        }
        public void LoadDynamicView()
        {
            DynamicItemCard = new DynamicItemCard(DynamicCards);
            dynamicInfoWindow.AddView(DynamicItemCard.ControlContent);
        }
        public async Task Like()
        {
            try
            {
                var result = await Client.Like(DynamicCards);
                if (result.Success)
                {
                    var obj = result.Data;
                    if (obj.IsNotEmpty())
                    {
                        if (obj["code"].ToInt32() == 0)
                        {
                            if (DynamicCards.Desc.Is_Liked == 0)
                            {
                                DynamicCards.Desc.Is_Liked = 1;
                                DynamicCards.Desc.Like += 1;
                            }
                            else
                            {
                                DynamicCards.Desc.Is_Liked = 0;
                                DynamicCards.Desc.Like -= 1;
                            }
                        }
                        else
                            PopupToast.Show("点赞失败!");
                    }
                }
                else
                    PopupToast.Show(result.Message);
            }
            catch (Exception ex)
            {
                LogManager.Instance.LogError("Like", ex);
            }
        }
    }
}